{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e7f3f1b6",
   "metadata": {},
   "source": [
    "# 使用Matlab数组\n",
    "我们知道NumPy为我们提供了以Python的可读格式来保存数据的方法。但是SciPy也为我们提供了与Matlab的互操作性。\n",
    "\n",
    "SciPy为我们提供了scipy.io模块，它具有与Matlab数组一起工作的功能。\n",
    "\n",
    "# 以Matlab格式导出数据\n",
    "savemat()函数允许我们以Matlab格式导出数据。\n",
    "\n",
    "该方法需要以下参数。\n",
    "\n",
    "filename - 用于保存数据的文件名。\n",
    "mdict - 一个包含数据的字典。\n",
    "do_compression - 一个布尔值，指定是否对结果进行压缩。默认为假。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "da1b0bdc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy import io\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7b96f77d",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.arange(10)\n",
    "\n",
    "io.savemat('arr.mat', {\"vec\": arr})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b52f2f8",
   "metadata": {},
   "source": [
    "# 从Matlab格式导入数据\n",
    "loadmat()函数允许我们从Matlab文件中导入数据。\n",
    "\n",
    "该函数需要一个必要的参数。\n",
    "\n",
    "filename - 保存数据的文件名。\n",
    "\n",
    "它将返回一个结构化数组，其键是变量名称，相应的值是变量值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d0717123",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'__header__': b'MATLAB 5.0 MAT-file Platform: nt, Created on: Wed Jul  7 15:15:52 2021', '__version__': '1.0', '__globals__': [], 'vec': array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])}\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9,])\n",
    "\n",
    "# Export:\n",
    "io.savemat('arr.mat', {\"vec\": arr})\n",
    "\n",
    "# Import:\n",
    "mydata = io.loadmat('arr.mat')\n",
    "\n",
    "print(mydata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b4488c1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4 5 6 7 8 9]]\n"
     ]
    }
   ],
   "source": [
    "print(mydata['vec'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "74354545",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "# 我们可以看到，这个数组原本是一维的，但在提取时增加了一维。\n",
    "# 为了解决这个问题，我们可以传递一个额外的参数squeeze_me=True。\n",
    "\n",
    "mydata = io.loadmat('arr.mat', squeeze_me=True)\n",
    "\n",
    "print(mydata['vec'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
